﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EmotionRecognitionMessages;
using EmotionRecognitionEntities;////

namespace EmotionRecognitionGui
{
    

    public partial class InitialEndowment : BasicForm
    {

        Messages initialEndowMsg;
        GuiManager guiManager;
        Dictionary<int, Picture> DatabasepictureDictionary;
        List<Picture> guiPictureDictionary= new List<Picture>() ;
        Picture currentDisplayedPicture;
        List<Chunk> chunklist;
        int i = 0;
        bool secondRun;

        public InitialEndowment()
        {
            try
            {
                this.guiManager = GuiManager.Instance;
                initialEndowMsg = new Messages();
                secondRun = false;
                InitializeComponent();
            }
            catch (Exception e)
            {
                MessageBox.Show("Error: " + e.ToString());
            }

        }
        //the function when reentering this GUI
        public void ReEnter()
        {
            emotionListBox.Items.Clear();
            FillEmotionCategory();
            ShowFirstImage();
        }

        public void FillEmotionCategory()
        {

            ReplyMsg reply = initialEndowMsg.GetChunkslables();

            if (reply.MessageState == true)
            {
              //Get list of emotions from Replymsg
                chunklist = (List<Chunk>)reply.ParamArray[0];
                foreach (Chunk item in chunklist)
              {
              emotionListBox.Items.Add(item.Label);
              }
            }
             
        }

        /// <summary>
        /// Load first Image to PictureBox; 
        /// </summary>
        public void ShowFirstImage()
        {
            ReplyMsg reply = initialEndowMsg.LoadPicutersFromDB(0);
            DatabasepictureDictionary = (Dictionary<int, Picture>)reply.ParamArray[0];
            CopyPictures();
            if (guiPictureDictionary.Count != 0)
            {
                this.acceptEmotionButton.Enabled = true;
                this.createEmotionbutton.Enabled = true; 
                pictureBox1.Image = guiPictureDictionary.First().PictureImage;
                currentDisplayedPicture = guiPictureDictionary.First();
                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            }
            else
            {
                pictureBox1.Image = null;
                emotionListBox.Enabled = false;
                emotionListBox.Items.Clear();
                createEmotionTextBox.Enabled = false;
                initialEndowmnetLabel.Visible = true;
                acceptEmotionButton.Enabled = false;
                createEmotionbutton.Enabled = false;
            }
 
        }

        protected void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        protected void backButton_Click(object sender, EventArgs e)
        {
            guiManager.SwitchForms(this, guiManager.mainMenuExpertGui);

        }

        protected void acceptEmotionButton_Click(object sender, EventArgs e)
        {

            //Send info to database
            initialEndowMsg.AcceptEmotion(GetChunkIDByLabel((string)emotionListBox.SelectedItem), currentDisplayedPicture,guiManager.currentuserID);

          
            //reset pictures counter and begin again to show relevant pictures to user
           
           
            showImage();

        

            

         

            //if pictures  is not consistent - show it to the user
          /*  if (guiPictureDictionary[i].IsChecked != 2)
            {
                showImage();
            }
            */
         //   IsDisplayNeeded();
        }

        protected void  showImage()
        {
            ReplyMsg reply = initialEndowMsg.LoadPicutersFromDB(0);
            DatabasepictureDictionary = (Dictionary<int, Picture>)reply.ParamArray[0];
            CopyPictures();
          /*  if (currentDisplayedPicture.IsChecked == 2)
            {
                guiPictureDictionary.Remove(currentDisplayedPicture);

            }
            */
            //move to next Picture
            i++;

            if (i >= guiPictureDictionary.Count)
            {

                i = 0;
            }

            if (guiPictureDictionary.Count!=0)
            {
            this.acceptEmotionButton.Enabled = true;
            this.createEmotionbutton.Enabled = true;
            pictureBox1.Image = guiPictureDictionary[i].PictureImage;
            currentDisplayedPicture = guiPictureDictionary[i];
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            }

            if (guiPictureDictionary.Count ==0)
            {
             pictureBox1.Image = null;
             this.initialEndowmnetLabel.Visible = true;
             this.acceptEmotionButton.Enabled = false;
             this.createEmotionbutton.Enabled = false;
             this.emotionListBox.Enabled = false;
             initialEndowMsg.SetIndexCase(secondRun);
            }
        
           
        }

       
        /// <summary>
        /// check if all pictures all consistent 
        /// if all consistent - dont display nothing to the user
        /// </summary>
    /*    protected void IsDisplayNeeded()
        {
          
            int counter=0;

            foreach (Picture picture in guiPictureDictionary)
            {
                if (picture.isChecked==2)
                {
                    counter++;
                    
                }

            }

            if (guiPictureDictionary.Count==counter)
            pictureBox1.Image = null;


        }
        */
        protected void createEmotionbutton_Click(object sender, EventArgs e)
        {

            initialEndowMsg.CreateNewChunk(createEmotionTextBox.Text);
            emotionListBox.Items.Clear();
            FillEmotionCategory();
        }


        protected void CopyPictures()
        {
            guiPictureDictionary.Clear();
            foreach (Picture picture in DatabasepictureDictionary.Values)
            {
                if (picture.IsChecked != 2)
                {
                    guiPictureDictionary.Add(picture);

                }


            }

            if (guiPictureDictionary.Count == 0)
            {

                pictureBox1.Image = null;
            }
        }

        protected void buttonBack_Click_1(object sender, EventArgs e)
        {
            guiManager.SwitchForms(this, guiManager.mainMenuExpertGui);
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void checkBoxRound_CheckedChanged(object sender, EventArgs e)
        {
            secondRun = (secondRun == false) ? true : false;
        }

        private int GetChunkIDByLabel(string Label)
        {
            foreach (Chunk chunk in chunklist)
            {
                if (chunk.Label == Label)
                    return chunk.ChunkId;
                 
            }

            return -1;

        }

    }
}
